
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
     <title>应用管理</title>
     <#include "../StaticFile.ftl">
    <script>
    
    var toolbar = [{
            text:'新增应用',
            iconCls:'icon-add',
            handler:function(){$('#dlg').dialog('open');
            global.id=0;
            clean();
            }
        }];
    
    </script>
</head>
<body>


    
    <div id="cc" class="easyui-layout" style="width:100%;height:850px;">
     <#include "../Head.ftl">
        <#include "../Menu.ftl">
    <div   data-options="region:'center',title:'数据显示区',split:true"  >
    
    <div style="margin-top:10px;padding-bottom:10px;">
    <table>
    <tr>
    <td> 应用id:</td><td><input class="easyui-textbox" type="text" id="search_id" name="search_id" ></input></td>
    <td>应用名称:</td><td><input class="easyui-textbox" type="text" id="search_title" name="search_title" ></input></td>
    <td>应用包名:</td><td><input class="easyui-textbox" type="text" id="search_packagename" name="search_packagename" ></input></td>
    
    <td> 应用厂商:</td>
     <td><select class="easyui-combobox" name="search_company_id" id="search_company_id" style="width:180px;">
                       </select></td>
                       </tr>
     <tr><td>
      应用分类:</td>
      <td>
        <select class="easyui-combobox" name="search_category_id" id="search_category_id" style="width:100px;">
                       
                        
                        </select></td>
                        <td>
                        应用状态: </td>
                        <td>
                        <select class="easyui-combobox" name="search_status" id="search_status" style="width:100px;">
                       
                        <option value="-1">请选择</option>
                        <option value="0">正常</option>
                         <option value="1">禁用</option>
                        </select></td>
      <td> 应用来源:</td><td>
        <select class="easyui-combobox" name="search_source" id="search_source">
        <option value="-1">请选择</option>
                         <option value="0">未知</option>
                        <option value="1">抓取</option>
                         <option value="2">运营</option>
                          <option value="3">360</option>
                           <option value="4">华为</option>
                        </select></td>
                      
                        
                <td><select class="easyui-combobox" name="search_big_category" id="search_big_category">
        <option value="-1">请选择</option>
         <option value="1">应用</option>
         <option value="2">游戏</option>
        
        </select></td>  <td>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="doSearch()">查询</a> </td></tr>
    
    </div>
    <table id="dg" title="应用管理" style="width:100%" data-options="
               
                singleSelect:true,
                autoRowHeight:false,
                pagination:true,
                url:'/app/getdata',
                pageSize:20,toolbar:toolbar" >
        <thead>
            <tr>
                <th field="id" width="80" align="center">应用编号</th>
                <th data-options="field:'icon',width:60,align:'center',formatter:formatIcon" >应用图标</th>
                <th field="title" width="180" align="center">应用名称</th>
                <th field="packageName" width="180" align="center">应用包名</th>
                 <th  data-options="field:'type',width:80,align:'center',formatter:formatType">应用/游戏</th>
                 <th field="categoryName" width="100" align="center">分类名称</th>
                <th field="companyName" width="200" align="center">应用厂商</th>
                <th field="versionName" width="100" align="center">应用版本</th>
                <th data-options="field:'source',width:80,align:'center',formatter:formatSource" >应用包来源</th>
                <th data-options="field:'creatdate',width:160,align:'center',formatter:formatCreatDate"  >添加时间</th>
                 <th data-options="field:'downloadcount',width:70,align:'center'"  >下载次数</th>
                <th data-options="field:'status',width:80,align:'center',formatter:formatStatus" >应用状态</th>
                 <th data-options="field:'downloadUrl',width:80,align:'center',formatter:formatUrl" >下载链接</th>
                <th data-options="field:'operation',width:150,align:'center',formatter:formatOperation" >操作</th>
            </tr>
        </thead>
    </table>
   
  
    
    <div id="dlg" class="easyui-dialog" title="添加应用包" data-options="resizable:true,iconCls:'icon-save'" style="width:550px;height:750px;padding:10px;">
       
            <table cellpadding="5">
                <tr>
                    <td>应用图标:</td>
                    <td>
                   <#assign name="test">
                   <#assign img_flag="0">
                   <#include "../FileUpload.ftl">
                 
                    </td>
                </tr>
                <tr>
               
                    <td>应用名称:</td>
                    <td><input class="easyui-textbox" type="text" id="title" name="title" ></input></td>
                </tr>
                <tr>
                    <td>英文名称:</td>
                    <td><input class="easyui-textbox" type="text"  id="enname" name="enname" ></input></td>
                </tr>
                <tr>
                    <td>应用厂商:</td>
                    <td>
                     <select class="easyui-combobox" name="company_id" id="company_id" style="width:180px;">
                     <option value="0">请选择</option>
                     
                       </select>
                    </td>
                </tr>
                <tr>
                    <td>应用分类:</td>
                    <td>
                        <select class="easyui-combobox" name="category_id" id="category_id" style="width:100px;">
                        </select>
                    </td>
                </tr>
                 <tr>
                    <td>应用标签:</td>
                      <td><select  class="easyui-combobox" id="tags" name="tags" data-options="multiple:true,multiline:true" id="tags" style="width:200px;height:50px">
                      
              
                      </select>
                    
                  </td>
                </tr>
                 <tr>
                    <td>应用介绍:</td>
                    <td><input class="easyui-textbox" id="description" name="description" data-options="multiline:true" style="width:260px;height:100px"></input></td>
                </tr>
                <tr>
                    <td>应用截图:</td>
                    <td>
                    <#assign name="screenshots_one">
                    <#assign img_flag="1">
                   <#include "../FileUpload.ftl">
                   
                   </td>
                    
                </tr>
                <tr>
                <td>
                </td>
                    <td>
                     <#assign name="screenshots_two">
                    <#assign img_flag="1">
                   <#include "../FileUpload.ftl">
                   
                   </td>
                
                </tr>
                
                 <tr>
                <td>
                </td>
                    <td>
                     <#assign name="screenshots_three">
                    <#assign img_flag="1">
                   <#include "../FileUpload.ftl">
                   
                   </td>
                
                </tr>
                 <tr>
                <td>
                </td>
                    <td>
                     <#assign name="screenshots_four">
                    <#assign img_flag="1">
                   <#include "../FileUpload.ftl">
                   
                   </td>
                
                </tr>
                 <tr>
                <td>
                </td>
                    <td>
                     <#assign name="screenshots_five">
                    <#assign img_flag="1">
                   <#include "../FileUpload.ftl">
                   
                   </td>
                
                </tr>
                 <tr>
                <td>
                应用上传:
                </td>
                    <td>
                    <form id="form_upload_apk" method="post" enctype="multipart/form-data" action="/uploadapk">
                    <input class="easyui-filebox" name="file" data-options="prompt:'Choose a file...'" style="width:40%">
                    <input type="submit" value="上传">
                    <img  src="/css/images/loading.gif" id="loadding_apk_${name}"style="display:none" id="loading" />
                   </form>
                     <span id="form_upload_successful_apk" style="display:none;">
                     <a href="" id="form_upload_successful_url">应用下载</a>
                        <a href="javascript:void(0)" id="reupload_apk" class="easyui-linkbutton" onclick="">重新上传</a>
                    </span>
                   </td>
                
                </tr>
                 <tr>
                    <td>应用包名:</td>
                    <td>
                    <input class="easyui-textbox" name="packageName" id="packageName" readonly="true"  >
                   
                   </td>
                    
                </tr>
                  <tr>
                    <td>应用版本名称:</td>
                    <td>
                    <input class="easyui-textbox" name="versionName" id="versionName" readonly="true"  >
                   
                   </td>
                    
                </tr>
                  </tr>
                  <tr>
                    <td>应用版本号:</td>
                    <td>
                    <input class="easyui-textbox" name="versionCode" id="versionCode" readonly="true" >
                   
                   </td>
                    
                </tr>
                <tr>
                    <td>应用包大小:</td>
                    <td>
                    <input class="easyui-textbox" name="bytes" id="bytes" readonly="true" >
                   
                   </td>
                    
                </tr>
                
                <tr>
                    <td>应用来源:</td>
                    <td>
                        <select class="easyui-combobox" name="source" id="source">
                         <option value="0">未知</option>
                        <option value="1">抓取</option>
                         <option value="2">运营</option>
                          <option value="3">360</option>
                           <option value="4">华为</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>应用包更新时间:</td>
                    <td>
                    <input class="easyui-textbox" name="message"  >
                   
                   </td>
                    
                </tr>
                 <tr>
                    <td>应用星级:</td>
                    <td>
                    <input class="easyui-textbox" name="star"  id="star"   >
                   
                   </td>
                    
                </tr>
                 <tr>
                    <td>编辑评论:</td>
                    <td>
                    <input class="easyui-textbox" name="editcomment" id="editcomment" data-options="multiline:true" style="width:260px;height:100px" >
                   
                   </td>
                    
                </tr>
            </table>
       
         <div style="text-align:center;padding:5px">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()">添加</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#dlg').dialog('close');">取消</a> 
         </div>
        </div>
       
      </div>
      <div>
     <script>
        
        $.extend($.fn.dialog.methods, {
            mymove: function(jq, newposition){
            return jq.each(function(){
            $(this).dialog('move', newposition);
        });
    }
    });
          $(function(){
          
          $('#dlg').dialog('mymove', {
                left: 200,
              top: 100
                });
           $('#dlg').dialog('close');
            
              $.ajax({ type: "GET",
                url: "/category/getall",
                success: function(data)
                 {
                    var returnData=jQuery.parseJSON(data);
                    if(returnData.code==0)
                    {
                     
                        var categorys=returnData.rows;
                        var category_index={"title":"请选择","id":"0",selected:true};
                        categorys.splice(0, 0, category_index);
                        $('#search_category_id').combobox('select',categorys[0].id);
                        $('#search_category_id').combobox({
                            valueField:"id",
                            textField:"title",
                            data:categorys
                        });
                         //$('#category_id').combobox('setValue', categorys[0].id);
                        $('#category_id').combobox({
                            valueField:"id",
                            textField:"title",
                            data:categorys
                        });
                        
                    }
                  }});
                  
                   $.ajax({ type: "GET",
                url: "/company/getall",
                success: function(data)
                 {
                    var returnData=jQuery.parseJSON(data);
                    var companys=returnData.rows;
                    var companyIndex={"companyName":"请选择","id":"0",selected:true};
                    companys.splice(0, 0, companyIndex)
                    if(returnData.code==0)
                    {
                        $('#search_company_id').combobox({
                            valueField:"id",
                            textField:"companyName",
                            data:companys
                        });
                        $('#company_id').combobox({
                            valueField:"id",
                            textField:"companyName",
                            data:companys
                        });
                        
                    }
                  }});
                  
                   $.ajax({ type: "GET",
                url: "/tags/getall",
                success: function(data)
                 {
                    var returnData=jQuery.parseJSON(data);
                    if(returnData.code==0)
                    {
                        $('#tags').combobox({
                            valueField:"id",
                            textField:"tags",
                            data:returnData.rows
                        });
                       
                        
                    }
                  }});
            
            var pager = $('#dg').datagrid().datagrid('getPager');
             });
               // get the pager of datagrid
               
         function doSearch(){
            $('#dg').datagrid('load',{
            "id":$("#search_id").textbox("getValue"),
            "title":$("#search_title").textbox("getValue"),
            "search_company_id":$("#search_company_id").combobox("getValue"),
            "search_category_id":$("#search_category_id").combobox("getValue"),
            "search_source":$("#search_source").combobox("getValue"),
            "search_status":$("#search_status").combobox("getValue"),
            "search_packagename":$("#search_packagename").textbox("getValue"),
            "search_big_category":$("#search_big_category").textbox("getValue")
            
            });
           /* var data=$('#dg').datagrid("getData");
            if(data.code&&data.code==2)
            {
                window.location.href="/user/login";
            }*/
            }
            
            /*(function() {
             
            $('#form_upload_successful').click(function()
                {
                    $('#form_upload').show();
                    $("#form_upload_successful").hide();
                    
                }
            );
           $('#form_upload').ajaxForm({
               
                success: function() {
                  
                  //alert(1)
                },
                complete: function(xhr) {
                    
                    var display_area=$("#form_upload_successful");
                    if(xhr.responseText&&jQuery)
                    {
                         var result=jQuery.parseJSON(xhr.responseText);
                         display_area.show();
                         $('#form_upload').hide();
                         $('#display_img').attr("src",result.filepath);
                    }
                    //status.html(xhr.responseText);
                }
            }); 

        })();*/   
        
     var global={"downLoadUrl":"","crc32":"","md5":""}; 
     (function() {
             
           
            
            
                $('#reupload_apk').click(
                
                function()
                {
                    $('#form_upload_apk').show();
                    $("#form_upload_successful_apk").hide();
                    global.downLoadUrl="";
                    global.crc32="";
                    global.md5="";
                }
                )
            $('#form_upload_apk').ajaxForm({
               
                beforeSend: function(e) {
                    $("#loadding_apk_${name}").show();
               },
                success: function() {
                  
                  //alert(1)
                },
                
            
                complete: function(xhr) {
                    
                    var display_area=$("#form_upload_successful_apk");
                    if(xhr.responseText&&jQuery)
                    {
                         
                         var result=jQuery.parseJSON(xhr.responseText);
                         //$('#display_img_${name}').attr("src",result.filepath);
                         if(result.code==0)
                         {
                         $("#loadding_apk_${name}").hide();
                         $('#form_upload_apk').hide();
                         display_area.show();
                         $("#packageName").textbox("setValue",result.packageName);
                         
                          $("#versionName").textbox("setValue",result.appVersion);
                          $("#versionCode").textbox("setValue",result.appVersionCode);
                           $("#bytes").textbox("setValue",result.appbyte);
                            global.downLoadUrl=result.filepath;
                            global.crc32=result.crc32;
                            global.md5=result.md5;
                            $("#form_upload_successful_url").attr("href",global.downLoadUrl);
                           }
                    }
                    //status.html(xhr.responseText);
                }
            }); 

        })();
        
        function submitForm()
        {
        
           var title=$("#title").textbox("getValue");
           if(!title)
           {
                alert("名称不能为空");
                return;
           }
           var enname=$("#enname").textbox("getValue");
          
           var icon=$("#display_img_test").attr("src");
           if(!icon){
           
             alert("请上传图标");
            return;
           }
           if(!global.downLoadUrl) {
          
                alert("请上传apk文件");
                return;
           }
           var company_id=$("#company_id").combobox("getValue");
           if(company_id==0) {
          
                alert("请选择所属公司");
                return;
           }
           var category_id=$("#category_id").combobox("getValue");
           if(category_id==0){
              alert("请选择分类信息");
              return;
           }
           var screenshots_one=$("#display_img_screenshots_one").attr("src");
           var screenshots_two=$("#display_img_screenshots_two").attr("src");
           var screenshots_three=$("#display_img_screenshots_three").attr("src");
           var screenshots_four=$("#display_img_screenshots_four").attr("src");
           var screenshots_five=$("#display_img_screenshots_five").attr("src");
           if(!screenshots_one&&!screenshots_two&&!screenshots_three&&!screenshots_four&&!screenshots_five){
           
                alert("请上传至少一张张截图");
                return;
           }
           var tagsValues=$("#tags").combobox("getValues");
    
            if(!window.confirm("确定要保存么？"))
                {
                    return;
                }
            $.ajax({
                type: "POST",
                  url: "/app/add",
                  data: {
                  "id":global.id,
                   "title":title,
                   "enname":enname,
                   "versionName":$("#versionName").textbox("getValue"),
                   "versionCode":$("#versionCode").textbox("getValue"),
                    "packageName":$("#packageName").textbox("getValue"),
                    "bytes":$("#bytes").textbox("getValue"),
                    "description":$("#description").textbox("getValue"),
                    "icon":icon,
                    "downloadUrl":global.downLoadUrl,
                    "company_id":company_id,
                    "category_id":category_id,
                    "tags":tagsValues.join(),
                    "screenshots_one":$("#display_img_screenshots_one").attr("src"),
                    "screenshots_two":$("#display_img_screenshots_two").attr("src"),
                    "screenshots_three":$("#display_img_screenshots_three").attr("src"),
                    "screenshots_four":$("#display_img_screenshots_four").attr("src"),
                    "screenshots_five":$("#display_img_screenshots_five").attr("src"),
                    "source":$("#source").combobox("getValue"),
                    "crc32":global.crc32,
                    "md5":global.md5,
                    "star":$("#star").textbox("getValue"),
                    "editcomment":$("#editcomment").textbox("getValue")
                   },
                  success: function(data)
                  {
                
                     var returnData=jQuery.parseJSON(data);
                    if(returnData.code==0)
                    {
                        doSearch();
                         $('#dlg').dialog('close');
                    }
                    else
                    {
                        alert(returnData.message)
                    }
                  
                  
                  }
});
        
        }
        function formatIcon(val,row)
        {
           return "<img style='high:30px;width:30px' src='"+val+"'/>";
        }
        function formatStatus(val,row)
        {
            if(val==0)
            {
                return "正常"
            }
            else
            {
                return "禁用"
            }
        }
        function formatOperation(val,row)
        {
        
           
            if(row.status==0)
            {
                return '<a href="javascript:void(0)" class="easyui-linkbutton" style="padding-right:5px;" onclick="upadateStatus('+row.id+',1)">禁用</a><a href="javascript:void(0)" class="easyui-linkbutton"  onclick="editElement('+row.id+')">编辑</a>'
            }
            else
            {
                return '<a href="javascript:void(0)"  style="padding-right:5px;" onclick="upadateStatus('+row.id+',0)">启用</a><a href="javascript:void(0)" class="easyui-linkbutton" onclick="editElement('+row.id+')">编辑</a>'
               
            }
           
        }
        
         function upadateStatus(id,status)
            {
                if(!window.confirm("确定要修改么？"))
                {
                    return;
                }
                $.ajax({ type: "GET",
                url: "/app/changestatus?id="+id+"&status="+status,
                success: function(data)
                 {
                    var returnData=jQuery.parseJSON(data);
                    if(returnData.code==0)
                    {
                     $('#dlg').dialog('close');
                        doSearch(); 
                        
                      // alert("chenggong");
                    }
                  }});
            }
        
        
        function formatCreatDate(val,row)
        {
        
            return formatTime(val);
        }
        function formatSource(val,row)
        {  
            var map={"0":"未知","1":"抓取","2":"运营","3":"360","4":"华为"}
            val=val+"";
            return map[val];
        }
        
        function formatUrl(val,row)
        {
            return "<a href='"+val+"'>下载包</a>"
        }
        
        function formatType(val,row){
            var value="未知";
            switch(val){
                case "1":
                    value="应用"
                    break;
                case "2":
                    value="游戏";
                    break;
            }
            return value;
        }
        var global={"id":0};
        function editElement(elementId)
        {
        
            global.id=elementId;
            $.ajax({ type: "GET",
                url: "/app/getelement?id="+elementId,
                success: function(data)
                 {
                    var returnData=jQuery.parseJSON(data);
                    if(returnData.code==0)
                    {
                    
                    clean();
                     $('#dlg').dialog('open');
                     var element=returnData.data;
                     $("#title").textbox("setValue",element.title);
                     $("#enname").textbox("setValue",element.enname)
                     $("#versionName").textbox("setValue",element.versionName);
                      $("#versionCode").textbox("setValue",element.versionCode);
                      $("#packageName").textbox("setValue",element.packageName);
                     $("#bytes").textbox("setValue",element.bytes);
                      $("#description").textbox("setValue",element.description);
                      $("#display_img_test").attr("src",element.icon);
                       $("#company_id").combobox("setValue",element.company_id);
                        $("#category_id").combobox("setValue",element.category_id);
                        var screenshots=element.screenshots.split(";");
                       
                           switch(screenshots.length){
                           
                           case 1:
                           $("#display_img_screenshots_one").attr("src",screenshots[0]);
                           
                           break;
                           case 2:
                           $("#display_img_screenshots_one").attr("src",screenshots[0]);
                           $("#display_img_screenshots_two").attr("src",screenshots[1]);
                           break;
                           case 3:
                            $("#display_img_screenshots_one").attr("src",screenshots[0]);
                           $("#display_img_screenshots_two").attr("src",screenshots[1]);
                             $("#display_img_screenshots_three").attr("src",screenshots[2]);
                               break;
                               
                            case 4:
                             $("#display_img_screenshots_one").attr("src",screenshots[0]);
                           $("#display_img_screenshots_two").attr("src",screenshots[1]);
                             $("#display_img_screenshots_three").attr("src",screenshots[2]);
                               $("#display_img_screenshots_four").attr("src",screenshots[3]);
                               break;
                            default:
                              $("#display_img_screenshots_one").attr("src",screenshots[0]);
                           $("#display_img_screenshots_two").attr("src",screenshots[1]);
                             $("#display_img_screenshots_three").attr("src",screenshots[2]);
                               $("#display_img_screenshots_four").attr("src",screenshots[3]);
                                 $("#display_img_screenshots_five").attr("src",screenshots[4]);
                             }
                       
                        $('#tags').combobox('setValues',element.tags);
                        $("#source").combobox("setValue",element.source);
                        global.downLoadUrl=element.downloadUrl;
                          $("#form_upload_successful_url").attr("href",global.downLoadUrl);
                        global.crc32=element.crc32;
                         global.md5=element.md5;
                        $("#star").textbox("setValue",element.star);
                        $("#editcomment").textbox("setValue",element.editcomment);
                        (function()
                        {
                            $("#form_upload_test").hide();
                            $("#form_upload_successful_test").show();
                            $("#form_upload_screenshots_one").hide();
                            $("#form_upload_successful_screenshots_one").show();
                            $("#form_upload_screenshots_two").hide();
                            $("#form_upload_successful_screenshots_two").show();
                             $("#form_upload_screenshots_three").hide();
                            $("#form_upload_successful_screenshots_three").show();
                             $("#form_upload_screenshots_four").hide();
                            $("#form_upload_successful_screenshots_four").show();
                             $("#form_upload_screenshots_five").hide();
                            $("#form_upload_successful_screenshots_five").show();
                            $("#form_upload_apk").hide();
                            $("#form_upload_successful_apk").show();
                        })()
                 
                    
                      
                    }
              
                  }});
        }
        
        function clean()
        {
             (function()
                        {
                            $("#form_upload_test").show();
                            $("#form_upload_successful_test").hide();
                            $("#form_upload_screenshots_one").show();
                            $("#form_upload_successful_screenshots_one").hide();
                            $("#form_upload_screenshots_two").show();
                            $("#form_upload_successful_screenshots_two").hide();
                              $("#form_upload_screenshots_three").show();
                            $("#form_upload_successful_screenshots_three").hide();
                              $("#form_upload_screenshots_four").show();
                            $("#form_upload_successful_screenshots_four").hide();
                              $("#form_upload_screenshots_five").show();
                            $("#form_upload_successful_screenshots_five").hide();
                            
                              $('#form_upload_apk').show();
                    $("#form_upload_successful_apk").hide();
               })()
                $("#title").textbox("setValue","");
                     $("#enname").textbox("setValue","")
                     $("#versionName").textbox("setValue","");
                     $("#versionCode").textbox("setValue","");
                      $("#packageName").textbox("setValue","");
                     $("#bytes").textbox("setValue","");
                      $("#description").textbox("setValue","");
                      $("#display_img_test").attr("src","");
                       $("#company_id").combobox("setValue",0);
                        $("#category_id").combobox("setValue",0);
                        $("#display_img_screenshots_one").attr("src","");
                           $("#display_img_screenshots_two").attr("src","");
                            $("#display_img_screenshots_three").attr("src","");
                           $("#display_img_screenshots_four").attr("src","");
                            $("#display_img_screenshots_five").attr("src","");
                            $("#source").combobox("setValue",0);
                        $('#tags').combobox('setValues',[]);
                         $("#star").textbox("setValue","");
                          $("#editcomment").textbox("setValue","");
               
        }
      
      function createSource()
      {
        $('#dlg_source').dialog('open')
      }
    </script>
</body>
</html>